package com.example.lishishangdejintian;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<User> list=new ArrayList<>();
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                JSONObject object = new JSONObject(msg.obj.toString());
                String Today = object.getString("today");
                text1.setText(Today);
                JSONArray object1 = object.getJSONArray("result");
                for (int i=0; i<object1.length();i++){
                    JSONObject jsonObject= (JSONObject) object1.get(i);
                    String year=jsonObject.getString("year");
                    String title=jsonObject.getString("title");
                    User user=new User(year,title);
                    list.add(user);
                }
                listView.setAdapter(new Myadapter());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
    ListView listView;
    TextView text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list_view);
        text1 = (TextView) findViewById(R.id.date);
        getData();
    }

    public void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    URL url = new URL("http://www.ipip5.com/today/api.php?type=json");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String ss = null;
                        StringBuffer sb = new StringBuffer();
                        while ((ss = br.readLine()) != null) {
                            sb.append(ss);
                        }
                        br.close();
                        is.close();
                        Message message = handler.obtainMessage();
                        message.obj = sb;
                        handler.sendMessage(message);
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            User user=list.get(i);
            view=getLayoutInflater().inflate(R.layout.item,null);
            TextView text1,text2;
            text1= (TextView) view.findViewById(R.id.text_year);
            text2= (TextView) view.findViewById(R.id.text_titel);
            text1.setText(user.getYear());
            text2.setText(user.getTitle());
            return view;
        }
    }
}
